unit clsFeriado;

interface

uses Dm, dbtables, biblio, sysutils;

type
  TFeriado = class(TObject)
  public
    Fer_Descricao: string;
    function Gravar(p_Fer_Data: TdateTime): Byte;
    function Recuperar(p_Fer_Data: TdateTime): Byte;
    function Excluir(p_Fer_Data: TDatetime): Byte;
    function Proximo_DiaUtil(p_Fer_Data: TDatetime): TDatetime;
    procedure Consultar(var p_q: TQuery);
  end;

implementation

function TFeriado.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Feriado where Fer_Data=:k_Fer_Data');
  tq.parambyname('k_Fer_Data').asdatetime := p_Fer_Data;
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO Feriado VALUES(:k_Fer_Data,:k_Fer_Descricao)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE Feriado SET Fer_Descricao=:k_Fer_Descricao WHERE Fer_Data=:k_Fer_Data');
  end;
  tq.parambyname('k_Fer_Data').asdatetime := p_Fer_Data;
  tq.parambyname('k_Fer_Descricao').asstring := Fer_Descricao;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TFeriado.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Feriado where Fer_Data=:k_Fer_Data');
  tq.Params[0].asdatetime := p_Fer_Data;
  tq.open;
  if tq.eof then begin
    Fer_Descricao := '';
    Result := 0;
  end else begin
    Fer_Descricao := tq.fieldbyname('Fer_Descricao').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TFeriado.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Feriado WHERE Fer_Data=:k_Fer_Data');
  tq.parambyname('k_Fer_Data').asdatetime := p_Fer_Data;
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TFeriado.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Feriado order by Fer_Data');
end;

function TFeriado.Proximo_DiaUtil;
var
  tq: TQuery;
  dia_util: TDateTime;
  ok: boolean;
  dia: integer;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := false;
  dia_util := p_Fer_Data;
  ok := false;
  while not ok do begin
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Select * from Feriado where Fer_Data=:a');
    tq.params[0].asdatetime := dia_util;
    tq.open;
    if not tq.eof then begin
      dia_util := dia_util + 1;
      dia := DayOfWeek(dia_util);
      if dia = 1 then dia_util := dia_util + 1;
      if dia = 7 then dia_util := dia_util + 2;
    end else begin
      dia := DayOfWeek(dia_util);
      if (dia = 1) or (dia = 7) then begin
        if dia = 1 then dia_util := dia_util + 1;
        if dia = 7 then dia_util := dia_util + 2;
      end else ok := true;
    end;
  end;
  tq.close;
  tq.free;
  result := dia_util;
end;

end.
